package Dao.Process.Imp;

import Dao.Process.ReportDetailDao;
import Entity.Process.ReportDetail;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ReportDetailDaoImp implements ReportDetailDao {
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    @Override
    public ReportDetail getReportDetailById(Integer id, Connection conn) {
        ReportDetail reportDetail = null;
        String sql = "select" +
                " report_id, sence, develop, content, scheme, aim, datum, s_file_name, s_file_url, t_file_name, t_file_url" +
                " from report_detail where report_id = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                Integer reportId = rs.getInt(1);
                String sence = rs.getString(2);
                String develop = rs.getString(3);
                String content = rs.getString(4);
                String scheme = rs.getString(5);
                String aim = rs.getString(6);
                String datum = rs.getString(7);
                String sFileName = rs.getString(8);
                String sFileUrl = rs.getString(9);
                String tFileName = rs.getString(10);
                String tFileUrl = rs.getString(11);
                reportDetail = new ReportDetail(reportId, sence, develop, content, scheme, aim, datum, sFileName, sFileUrl, tFileName, tFileUrl);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return reportDetail;
    }

    @Override
    public Integer updateReportFile(String SNO, String fileName, String fileUrl, String type, Integer reportId, Connection conn) {
        int res = 0;
        String sql;
        if ("1".equals(type)) {
            sql = " update report_detail set s_file_name = ? , s_file_url = ?  where report_id = ?";
        } else {
            sql = " update report_detail set t_file_name = ? , t_file_url = ?  where report_id = ?";
        }
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, fileName);
            ps.setString(2, fileUrl);
            ps.setInt(3, reportId);
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return res;
    }
}
